package rexsee.multimedia;

import rexsee.core.utilities.Utilities;
import android.hardware.Camera;

public class ImageCaptureArguments {

	public String argumentsText = "";
	public int format = 256; //ImageFormat.JPEG, 2.2+;
	public int rotation = 0;
	public int width = 320;
	public int height = 240;
	public int jpegQuality = 90;
	public int jpegThumbnailWidth = 80;
	public int jpegThumbnailHeight = 60;
	public int jpegThumbnailQuality = 90;

	public boolean gpsData = false;
	public double altitude = -1;
	public double latitude = -1;
	public double longitude = -1;
	public long timeStamp = -1;
	public String processMethod = "";

	@Override
	public String toString() {
		String rtn = "";
		rtn += "\"format\":\"" + RexseeCameraPreview._getFormat(format) + "\"";
		rtn += ",\"rotation\":" + rotation;
		rtn += ",\"width\":" + width;
		rtn += ",\"height\":" + height;
		rtn += ",\"jpegQuality\":" + jpegQuality;
		rtn += ",\"jpegThumbnailWidth\":" + jpegThumbnailWidth;
		rtn += ",\"jpegThumbnailHeight\":" + jpegThumbnailHeight;
		rtn += ",\"jpegThumbnailQuality\":" + jpegThumbnailQuality;
		rtn += ",\"gpsData\":" + ((gpsData) ? "true" : "false");
		rtn += ",\"altitude\":" + altitude;
		rtn += ",\"latitude\":" + latitude;
		rtn += ",\"longitude\":" + longitude;
		rtn += ",\"timeStamp\":" + timeStamp;
		//rtn += ",\"processMethod\":\"" + processMethod + "\"";
		return "{" + rtn + "}";
	}
	public ImageCaptureArguments parseArguments(String arguments) {
		if (arguments == null || arguments.equals("")) return this;
		argumentsText = arguments;
		String[] argus = arguments.split(";");
		for (int i = 0; i < argus.length; i++) {
			if (argus[i].indexOf("=") <= 0) continue;
			String key = argus[i].substring(0, argus[i].indexOf("=")).trim();
			String value = argus[i].substring(argus[i].indexOf("=") + 1).trim();
			if (key.equalsIgnoreCase("format")) {
				format = RexseeCameraPreview._getFormat(value);
			} else if (key.equalsIgnoreCase("rotation")) {
				if (value.equals("90")) rotation = 90;
				else if (value.equals("180")) rotation = 180;
				else if (value.equals("270")) rotation = 270;
				else rotation = 0;
			} else if (key.equalsIgnoreCase("width")) {
				width = Utilities.getInt(value, 320);
			} else if (key.equalsIgnoreCase("height")) {
				height = Utilities.getInt(value, 240);
			} else if (key.equalsIgnoreCase("jpegQuality")) {
				jpegQuality = Utilities.getInt(value, 90);
			} else if (key.equalsIgnoreCase("jpegThumbnailWidth")) {
				jpegThumbnailWidth = Utilities.getInt(value, 90);
			} else if (key.equalsIgnoreCase("jpegThumbnailHeight")) {
				jpegThumbnailHeight = Utilities.getInt(value, 60);
			} else if (key.equalsIgnoreCase("jpegThumbnailQuality")) {
				jpegThumbnailQuality = Utilities.getInt(value, 90);
			} else if (key.equalsIgnoreCase("gpsData")) {
				gpsData = Utilities.getBoolean(value, false);
			} else if (key.equalsIgnoreCase("altitude")) {
				altitude = Utilities.getDouble(value, -1);
			} else if (key.equalsIgnoreCase("latitude")) {
				latitude = Utilities.getDouble(value, -1);
			} else if (key.equalsIgnoreCase("longitude")) {
				longitude = Utilities.getDouble(value, -1);
			} else if (key.equalsIgnoreCase("timeStamp")) {
				timeStamp = Utilities.getLong(value, -1);
			} else if (key.equalsIgnoreCase("processMethod")) {
				processMethod = value;
			}
		}
		return this;
	}

	public Camera setCamera(Camera camera) {
		Camera.Parameters paras = camera.getParameters();
		try {
			paras.setPictureFormat(format);
		} catch (Exception e) {
		}
		try {
			paras.setRotation(rotation);
		} catch (Exception e) {
		}
		try {
			paras.setPictureSize(width, height);
		} catch (Exception e) {
		}
		try {
			if (format == 256) { //ImageFormat.JPEG
				paras.setJpegQuality(jpegQuality);
				if (jpegThumbnailWidth > 0 && jpegThumbnailHeight > 0) {
					paras.setJpegThumbnailSize(jpegThumbnailWidth, jpegThumbnailHeight);
					paras.setJpegThumbnailQuality(jpegThumbnailQuality);
				}
			}
		} catch (Exception e) {
		}
		try {
			if (gpsData) {
				if (altitude > 0) paras.setGpsAltitude(altitude);
				if (latitude > 0) paras.setGpsLatitude(latitude);
				if (longitude > 0) paras.setGpsLongitude(longitude);
				if (timeStamp > 0) paras.setGpsTimestamp(timeStamp);
				else if (timeStamp == 0) paras.setGpsTimestamp(System.currentTimeMillis());
				//paras.setGpsProcessingMethod(processMethod);
			} else {
				paras.removeGpsData();
			}
		} catch (Exception e) {
		}
		try {
			camera.setParameters(paras);
		} catch (Exception e) {
		}
		return camera;
	}

}
